package dao

import (
	"account_project/entity"
	"account_project/models"
	"account_project/service"
	"context"
	"gitlab.whjzjx.cn/shanghai/kit/db"
)

func GetALl(ctx context.Context) ([]*entity.Contacts, error) {
	client, err := db.GetClient("AddressBookDB", "master")
	if err != nil {
		return nil, err
	}
	var records []*entity.Contacts
	err = client.Model(&entity.Contacts{}).Find(&records).Error
	if err != nil {
		return nil, err
	}
	return records, nil
}

func Login(name, password string, client *db.Engine) bool {
	var user models.User
	result := client.Where("Name = ?", name).First(&user)
	if result.Error != nil {
		return false // 用户不存在
	}

	inputHash := service.GenerateMD5(password)
	return inputHash == user.Password
}

//实现保存文件路径到数据库的函数

type DB struct {
	Error        error
	RowsAffected int64

	clone int
}

func SaveAvatarPath(username, path string, client *db.Engine) error {
	query := "UPDATE users SET avatar = ? WHERE name = ?"
	res := client.Exec(query, path, username)
	if res.Error != nil {
		return res.Error
	}
	return nil
}
